package web.services;

import web.bean.TenderNews;
import web.dao.SearchDao;
import web.dao.ToolDao;
import web.util.*;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tool.GetLocationFromTitle;

import java.util.*;

/**
 * Created by Administrator on 2016/7/1.
 */
@Service
public class SearchService {

    @Autowired
    private SearchDao searchDao;
    @Autowired
    private LocationService locationService;
    @Autowired
    private ToolDao toolDao;

    private final static Logger LOGGER= Logger.getLogger("SearchService.class");

    /**
     * 查询中标公司联系方式，与中标信息
     * @return
     */
    public Map<String,Object> findSuccessfulNewsBySubscriber(Map<String,Object> mapNews,int excelType){
        Map<String,Object> map = findNewsByWinBidCompany(mapNews.get("winBidCompany").toString());

        List<String> idList = (List<String>)mapNews.get("ids");
        List<Map<String,Object>> listNews =new ArrayList<Map<String, Object>>();
        if(idList == null || excelType == 2){
            map.put("listNews",listNews);
            return map;
        }
        Set<String> set =new HashSet<String>();
        for (String ids : idList) {
            String [] id = ids.split("_");
            Map<String,Object>successNews=toolDao.findSuccessfulNews(Integer.parseInt(id[0].toString()),id[1]);
            successNews.put("district_news_id",id[1]+" "+successNews.get("successful_tender_news_id"));

            if(successNews.get("title")!=null) {
                if(set.contains(successNews.get("title").toString())){
                    continue;
                }
                set.add(successNews.get("title").toString());

                successNews.remove("title_key");

                successNews.put("stime", successNews.get("successful_time")+"");
                successNews.put("starTime", DateUtil.getStringDate(Integer.parseInt(successNews.get("successful_time") + ""), "yyyy-MM-dd"));
                String district="";
                if(Integer.parseInt(successNews.get("provinceId").toString())>100000){
                    district+=locationService.getLocationMap().get(successNews.get("provinceId").toString());
                    if(Integer.parseInt(successNews.get("cityId").toString())>0){
                        String city=locationService.getLocationMap().get(successNews.get("cityId").toString())+"";
                        if(!city.equals("null") && !city.equals("")) {
                            district += "  " +city;
                        }
                    }
            /* 返回数据前对没有地域的地区进行地域处理 */
                }else{
                    int t_code= GetLocationFromTitle.getLocation(successNews.get("title").toString());
                    if(t_code!=0 ){
                        if(t_code%10000==0){
                            successNews.put("provinceId",t_code);
                            district+=locationService.getLocationMap().get(successNews.get("provinceId").toString());
                        }else{
                            successNews.put("cityId",t_code);
                            successNews.put("provinceId", t_code/10000*10000);
                            district+=locationService.getLocationMap().get(successNews.get("provinceId").toString());
                            district+=" "+locationService.getLocationMap().get(successNews.get("cityId").toString());
                        }
                    }
                }
                successNews.put("district",district);

                successNews.put("title",StringOperateUtil.reStrings(successNews.get("title")+""));
                successNews.put("winBidCompany",StringOperateUtil.reStrings(successNews.get("winBidCompany")+""));
                listNews.add(successNews);
            }


        }
//        map.put("listNews",adminLuceneSearchService.findSuccessfulNewsSubscriber(subscriber,winBidCompany));
        map.put("listNews",listNews);
        return map;
    }



    /**
     * 通过中标公司查询该公司所有的联系电话与联系人
     * @param winBidCompany
     * @return
     */
    public Map<String,Object> findNewsByWinBidCompany(String winBidCompany){
        return searchDao.findWinContactByCompany(MD5.md5(winBidCompany.trim()));

    }

    /**
     * 搜索中标公司并获取联系人
     * @param key
     */
    public Map<String,Map<String,Object>> insertWinBidCompanyContact(String key){
//        查询中标所有中标分析后的表格
        List<String> tables=searchDao.findTable("success_news%");
//        查询所有相关中标公司
        Set<String> set=new HashSet<String>();
        int num=0;
        for (String table : tables) {
            num++;
            if(num>2)break;
            searchDao.findWinBidCompany(table,StringOperateUtil.returnKey(key),set);
        }
        Map<String,Map<String,Object>> newsMap=new HashMap<String, Map<String, Object>>();
//        通过中标单位查询所有的联系人与电话
        num=0;
        for (String winBidCompany : set) {
            if(winBidCompany==null || winBidCompany.isEmpty())continue;
            Map<String,Object> map=findNewsByWinBidCompany(winBidCompany);
            if(map.get("com_phone")!=null || map.get("cellphone")!=null) {
                newsMap.put(winBidCompany, map);
                num++;
            }
            if(num>100){
                break;
            }
        }
        return newsMap;
//
    }


}
